import json
import os
from glob import glob
from pathlib import Path


def get_originals_and_fakes(root_dir):
    originals = []
    fakes = []
    for json_path in glob(os.path.join(root_dir, "*/metadata.json")):
        with open(json_path, "r") as f:
            metadata = json.load(f)
        for k, v in metadata.items():
            if v["label"] == "FAKE":
                fakes.append(k[:-4])
            else:
                originals.append(k[:-4])

    return originals, fakes


def get_all_original_video_paths(root_dir):
    originals = set()
    for json_path in glob(os.path.join(root_dir, "*/metadata.json")):
        json_path_parent = Path(json_path).parent
        with open(json_path, "r") as f:
            metadata = json.load(f)
        for k, v in metadata.items():
            original = v.get("original", None)
            if original is not None:
                originals.add(os.path.join(json_path_parent, original))
            else:
                originals.add(os.path.join(json_path_parent, k))
    originals = list(originals)
    return originals
